package io.renren.modules.region.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.region.entity.TCountryEntity;
import io.renren.modules.region.service.TCountryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 村域数据controller
 *
 * @author lhb
 * @date 2023-03-29 11:51:23
 */
@RestController
@RequestMapping("country")
public class TCountryController {
    @Autowired
    private TCountryService tCountryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("country:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = tCountryService.queryPage(params);

        return R.ok().put("page", page);
    }
    /**
     * 列表
     */
    @RequestMapping("/all")
    @RequiresPermissions("country:list")
    public R listAll() {
        QueryWrapper<TCountryEntity> queryWrapper = new QueryWrapper<>();
        List<TCountryEntity> countries = tCountryService.list(queryWrapper);

        return R.ok().put("data", countries);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{code}")
    public R info(@PathVariable("code") String code) {
        TCountryEntity tDevice = tCountryService.getById(code);

        return R.ok().put("country", tDevice);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("country:save")
    public R save(@RequestBody TCountryEntity country) {

        try {
            tCountryService.save(country);
            return R.ok();
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("country:update")
    public R update(@RequestBody TCountryEntity country) {
        tCountryService.updateById(country);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("country:delete")
    public R delete(@RequestBody String[] codes) {
        tCountryService.removeByIds(Arrays.asList(codes));

        return R.ok();
    }

}
